package main;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import packing.BlockRelation;
import packing.Box;
import packing.State;
import utility.FileTool;

public class Brutforce {

	private ArrayList<State> combination = new ArrayList<State>();
	private State main_state ;
	
	public void solve(String fileName) {
		main_state = FileTool.readBRSet(fileName);
		List<Box> readData = main_state.getBoxInputList();
		ArrayList<Box> boxSet = generateState(readData);
		getSubset(readData, boxSet);
		evaluationScore();
	}
	
	public ArrayList<Box> generateState(List<Box> readData){
		ArrayList<Box> boxSet = new ArrayList<Box>();
		System.out.print("Set: ");
		for(Box box : readData){			
			boxSet.add(box);
			for( int j = 0 ; j < readData.size() ; j++ ){
				if(!readData.get(j).getId().equals(box.getId())){
					String id = box.getId();
					int width = box.getWidth();
					int length = box.getLength();
					int height = box.getHeight();
					boxSet.add(new Box(id, width, length, height, BlockRelation.LEFT, readData.get(j)));
					boxSet.add(new Box(id, width, length, height, BlockRelation.INFRONT, readData.get(j)));
					boxSet.add(new Box(id, width, length, height, BlockRelation.TOP, readData.get(j)));
				}
			}
		}
		return boxSet;
	}
	
	public void getSubset(List<Box> readData, ArrayList<Box> boxSet){
		for(int i = 0 ; i < readData.size() ; i++){
			ArrayList<Box> clone = new ArrayList<Box>();
			for(int j = 0 ; j < boxSet.size() ; j++){
				if(!boxSet.get(j).getOriginalId().equals(readData.get(i).getOriginalId())){
					if(boxSet.get(j).getNextNode() != null)
						clone.add(boxSet.get(j));
				}
			}
			getSubsets(readData, readData.get(i), clone);
		}
	}

	public void getSubsets(List<Box> readData, Box box, ArrayList<Box> s){
		ArrayList<ArrayList<Box>> set = new ArrayList<ArrayList<Box>>();
		
	    int setSize = s.size();
	    int finalValue = (int) (Math.pow(2, setSize));
	    String bValue = "";
	    for (int i = 0; i < finalValue; i++) {
	        bValue = Integer.toBinaryString(i);
	        int bValueSize = bValue.length();
	        for (int k = 0; k < (setSize - bValueSize); k++) {
	            bValue = "0" + bValue;
	        }
	        ArrayList<Box> temp = new ArrayList<Box>();
	        for (int j = 0; j < setSize; j++) {
	            if (bValue.charAt(j) == '1') 	temp.add(s.get(j));
	        }
	        set.add(temp);
	    }
	    bValue="";
	    
	    for(ArrayList<Box> temp : set){
	    	boolean isDuplicate = false;
	    	for (int i = 0; i < temp.size(); i++) {
	    		for (int k = 0; k < temp.size(); k++) {
	    			if(!temp.get(i).getId().equals(temp.get(k).getId())){
	    				if(temp.get(k).getOriginalId().equals(temp.get(i).getOriginalId()))
	    					isDuplicate = true;
	    			}
	    			if(isDuplicate)	break;
		    	}
	    		if(isDuplicate)	break;
    		}
	    	if(isDuplicate == false){
	    		temp.add(0, box);
	    		State st = new State(main_state.getContainer(), temp);
	    		System.out.println(st.evaluate());
	    		FileTool.writeFile(st);
	    		combination.add(st);
//	    		dump(System.out, temp);
	    	}else  		continue;
	    }
	}
	
	public void dump(PrintStream out, ArrayList<Box> set) {
		out.print("{");
		boolean first = true;
		for (Box b : set) {
			if (first) {
				first = false;
			} else {
				out.print(", ");
			}
			out.printf(b.getId());
		}
		out.println("}");
	}
	
	public void evaluationScore(){

	}
}
